﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Data;
using DLTN.Entities;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Text;

public partial class ChuyenMucND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["Id"] != null)
            {
                //HienThiChuyenMuc(Request.Params["Id"].ToString());
                HienThiChuyenMuc(Convert.ToInt32(Request.Params["Id"].ToString()));
            }
        }
    }

    private void HienThiChuyenMuc(int Id)
    {
        // Lay ten Loai chuyen muc        

        ChuyenMuc ent_ChuyenMuc = DataRepository.ChuyenMucProvider.GetById(Id);
        TList<DLTN.Entities.SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.Find("Id = " + ent_ChuyenMuc.IdSubChuyenMuc);
        if (TList_SubChuyenMuc.Count > 0)
        {
            LoaiChuyenMuc ent_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(TList_SubChuyenMuc[0].IdLoaiChuyenMuc);
            if (ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToString().Equals("Media"))
            {
                lbTenLoaiCM.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToString();
                linkLoaiCM.NavigateUrl = "~/Media.aspx";
                lbTenSubCM.Text = TList_SubChuyenMuc[0].TenSubChuyenMuc.ToString();
                linkSubCM.NavigateUrl = "~/" + "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + ent_LoaiChuyenMuc.Id.ToString() + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
            }
            else if (ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToString().Equals("Dịch vụ"))
            {
                lbTenLoaiCM.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToString();
                linkLoaiCM.NavigateUrl = "~/DichVu.aspx";
                lbTenSubCM.Text = TList_SubChuyenMuc[0].TenSubChuyenMuc.ToString();
                linkSubCM.NavigateUrl = "~/" + "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + ent_LoaiChuyenMuc.Id.ToString() + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
            }
            else
            {
                lbTenLoaiCM.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToString();
                linkLoaiCM.NavigateUrl = "~/GioiThieu.aspx";
                lbTenSubCM.Text = TList_SubChuyenMuc[0].TenSubChuyenMuc.ToString();
                linkSubCM.NavigateUrl = "~/" + "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + ent_LoaiChuyenMuc.Id.ToString() + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
            }                        
        }

        lblTieuDe.Text = ent_ChuyenMuc.TieuDe;
        ltrTitle.Text = ent_ChuyenMuc.TieuDe;
        HienThiTags(ent_ChuyenMuc.TieuDe.ToString());

        lblNguon.Text = ent_ChuyenMuc.Nguon;
        int _view = ent_ChuyenMuc.LuotXem;
        
        litnoidung.Text = String.Format("<meta name=\"description\" content=\"{0}\" />", ent_ChuyenMuc.TomTat);
        lblNoidung.Text = Server.HtmlDecode(ent_ChuyenMuc.NoiDung.ToString());
        linkChuyenMuc.NavigateUrl = "~/" + "ChuyenMuc/" + ent_ChuyenMuc.Id.ToString() + "/" + ent_ChuyenMuc.IdSubChuyenMuc.ToString() + "/" + GetTieuDe(ent_ChuyenMuc.TieuDe.ToString()) + ".aspx";
        imageanhchuyenmuc.ImageUrl = "~/Upload/ChuyenMuc/" + ent_ChuyenMuc.AnhDaiDien.ToString();
        lbNgaythang.Text = ent_ChuyenMuc.NgayCapNhat.ToString();

        // Update luot xem
        _view += 1;
        lblLuotxem.Text = (_view).ToString();
        string query = string.Empty;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        conn.Open();
        query = "UPDATE ChuyenMuc SET LuotXem = '" + _view + "' where Id = '" + ent_ChuyenMuc.Id + "'";
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = conn;
        sqlCommand.CommandText = query;
        sqlCommand.ExecuteNonQuery();
        conn.Close();
        // end update luot xem        

        // lay danh sach cac tin lien quan
        int subcm_count = 0;
        string idSubCM_str = "IdSubChuyenMuc = " + ent_ChuyenMuc.IdSubChuyenMuc.ToString();
        TList<ChuyenMuc> list_CmLienquan = DataRepository.ChuyenMucProvider.GetPaged(idSubCM_str, "NgayCapNhat DESC", 0, 5, out subcm_count);
        dlChuyenmucLienquan.DataSource = list_CmLienquan;
        dlChuyenmucLienquan.DataBind();
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
    protected void cmdIn_Click(object sender, ImageClickEventArgs e)
    {
        Session["ctrl"] = pnNoiDung;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=600px,width=800px,scrollbars=1');</script>");
    }
    protected void cmdSendMail_Click(object sender, ImageClickEventArgs e)
    {
        TList<CauHinhMail> TList_CauHinhMail = DataRepository.CauHinhMailProvider.GetAll();
        if (TList_CauHinhMail.Count > 0)
        {
            MailMessage objEmail = new MailMessage();
            objEmail.From = new MailAddress(TList_CauHinhMail[0].TenMail);
            objEmail.To.Add(txtToEmail.Text);
            objEmail.Subject = txtTieuDe.Text; // Tiêu đề bài viết
            objEmail.SubjectEncoding = Encoding.UTF8;
            objEmail.BodyEncoding = Encoding.UTF8; //Chuyển bảng mã
            objEmail.Body = NoiDungMail(); //Chưa nội dung mail
            objEmail.IsBodyHtml = true;
            SmtpClient smtp;
            if (TList_CauHinhMail[0].TrangThai == true)
            {
                smtp = new SmtpClient() { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new System.Net.NetworkCredential(TList_CauHinhMail[0].TenMail, TList_CauHinhMail[0].MatKhau) };
            }
            else
            {
                smtp = new SmtpClient() { Host = "smtp.live.com", Port = 587, EnableSsl = true, Credentials = new System.Net.NetworkCredential(TList_CauHinhMail[0].TenMail, TList_CauHinhMail[0].MatKhau) };
            }
                smtp.Send(objEmail);
            WebMessage.Show("Chúng tôi đã gửi nội dung bài viết đến Thư điện tử cho bạn!");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "redirectMe", "alert('Chúng tôi đã gửi nội dung bài viết đến Thư điện tử cho bạn.\\n!')");
            //gọi đoạn javascrip để đóng cửa sổ lại

        }
    }

    private string NoiDungMail()
    {
        string NoiDung = "";
        NoiDung = "Đây là Mail gửi đến từ website của Du lịch Thanh Niên Xung Phong<br>";
        NoiDung += txtThongDiep.Text;
        NoiDung += "<br><a  href='" + Common.sPathUpLienKet + HttpContext.Current.Request.Url.AbsolutePath.ToString() + "'> Link từ website Du lịch Thanh niên xung phong </a>";
        NoiDung += "<br><br><hr>Vui lòng không trả lời Mail này!";
        return NoiDung;
    }

    private void HienThiTags(string title)
    {
        string[] k = GetTieuDe(title).Split('-');
        string strTags = "";
        foreach (string s in k)
        {
            if (s.Trim() != "-" && s.Trim() != "," && s.Trim() != "" && s.Trim() != "–")
            {
                strTags += "<a href='../../../ListChuyenMuc/" + s.Trim() + ".aspx' >";
                strTags += s.Trim() + "</a>, ";    
            }            
        }
        ltrTags.Text = strTags.Substring(0,strTags.Length-2);
    }
}